<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期" prop="dateInfo">
        <el-date-picker clearable v-model="queryParams.dateInfo" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择日期" :default-value="getDefaultDate()">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="渠道编号" prop="channelCode">
        <el-input v-model="queryParams.channelCode" placeholder="请输入渠道编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="渠道名称" prop="channelName" class="single-line-label">
        <span class="label-spacing"></span>
        <el-select v-readonlyRemove v-model="selectedChannelNames" multiple clearable placeholder="请选择渠道名称" filterable
          @change="updateChannelNames">
          <el-option v-for="(item, i) in extData.channelNameList || []" :key="i" :label="item.key"
            :value="item.key"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="baseInfoList">

      <el-table-column label="渠道信息" align="center" prop="channelName" width="250">
        <template slot-scope="scope">
          <span>
            {{ scope.row.channelName }} [{{ scope.row.channelCode }}]
            <span v-if="scope.row.priceType === '2'" style="color: red;">CPS</span>
          </span>
        </template>
      </el-table-column>

      <el-table-column label="数量" align="center" prop="number" />

      <el-table-column label="成本" align="center" prop="cost" />
      <el-table-column label="日期" align="center" prop="dateInfo" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small" type="text" icon="el-icon-edit" @click="handleDetailClick(scope.row)"
            v-hasPermi="['source:account:list']">
            明细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { listBaseInfo } from "@/api/source/channelData";
import { baseInfoExtData } from "@/api/source/baseInfo";
export default {
  name: "BaseInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      selectedChannelNames: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线索基本信息表格数据
      baseInfoList: [],
      extData: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opens: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        channelCode: null,
        channelName: null,
        dateInfo: this.getDefaultDate()
      },
      form: {},
    };
  },
  created() {
    this.getList();
    this.getOptions();
  },
  methods: {
    /** 查询线索基本信息列表 */
    getList() {
      this.loading = true;
      listBaseInfo(this.queryParams).then(response => {
        this.baseInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleDetailClick(row) {
      this.$router.push(`/source/pushdata?channelCode=${row.channelCode}`);
    },
    updateChannelNames() {
      this.queryParams.channelName = this.selectedChannelNames.join(',');
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.opens = false;
      this.reset();
    },
    getOptions() {
      baseInfoExtData().then((response) => {
        this.extData = response.data;
      });
    },

    getDefaultDate() {
      // 获取当天日期，并以 yyyy-MM-dd 格式返回
      const today = new Date();
      const year = today.getFullYear();
      const month = String(today.getMonth() + 1).padStart(2, '0');
      const day = String(today.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        channelCode: null,
        channelName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },




  }
};
</script>
